$(document).ready(function() {
	const $setPwdForm = $('#set-pwd-form')

	$setPwdForm.on().on('submit', function(event) {
		event.preventDefault()

		const username = $('#username').val()
		const password = $('#new-password').val()
		const confirmPassword = $('#confirm-password').val()

		if (password !== confirmPassword) {
			swal('两次输入的密码不一致', '', 'warning')
			return
		}

		const data = {
			username: username.trim(),
			password: password.trim()
		}

		$.ajax({
			type: 'POST',
			url: '/api/users/updatePwd',
			data: data,
			success: function(response) {
				const { code, msg } = response
				if (code !== 200) {
					return swal(msg, '', 'error')
				}
				swal(msg, '', 'success')
					.then(res => {
						if (res) {
							window.location.href = './login.html'
						}
					})
			}
		})
	})
})